<template>
    <div class='approval'>
      <el-card class="box-card" style="margin-top: 110px;">
          <div class='title'>
              {{$t('approval.title1')}}
          </div>
          <div class='menu-search'> 
              <div class='menu-title'>
                  <!-- 业务ID -->
                  <div class='searchItem'>
                      <span>{{$t('approval.orderId')}}</span>
                      <el-input v-model='listQuery.orderId' :placeholder="$t('rule.orderId')" size='small'></el-input>
                  </div>
                  <!-- 客户姓名 -->
                  <div class='searchItem'>
                      <span>{{$t('approval.realName')}}</span>
                      <el-input v-model='listQuery.realName' :placeholder="$t('rule.realName')" size='small'></el-input>
                  </div>
                  <!-- 手机号 -->
                  <div class='searchItem'>
                      <span>{{$t('approval.phoneNo')}}</span>
                      <el-input v-model='listQuery.phoneNo' :placeholder="$t('rule.phoneNo')" size='small'></el-input>
                  </div>
                  <!-- 申请时间 -->
                  <div class='searchItem searchItem-date'>
                      <span>{{$t('approval.time')}}</span>
                      <el-date-picker
                        size="small"
                        v-model="applyDate"
                        type="daterange"
                        :range-separator="$t('btn.go')"
                        :start-placeholder="$t('rule.beginDate')"
                        :end-placeholder="$t('rule.endDate')"
                        value-format="yyyy-MM-dd"
                        @change="dateChangeApplyDate"
                      ></el-date-picker>
                  </div>
                  <!-- 审批结果 -->
                  <div class='searchItem'>
                      <span>{{$t('approval.result')}}</span>
                      <el-select v-model='listQuery.approvalResult' size='small'>
                          <el-option
                          v-for='item in result'
                          :key='item.value'
                          :label='item.label'
                          :value='item.value'>
                          </el-option>
                      </el-select>
                  </div>
                  <!-- 是否资料退回 -->
                  <div class='searchItem'>
                      <span>{{$t('approval.isDataReturn')}}</span>
                      <el-select v-model='listQuery.dataReturn' size='small'>
                          <el-option
                          v-for='item in returnArr'
                          :key='item.value'
                          :label='item.label'
                          :value='item.value'>
                          </el-option>
                      </el-select>
                  </div>
                  <!-- 初/复审批结果 -->
                  <div class='searchItem'>
                      <span>{{$t('approval.firstReviewResult')}}</span>
                      <el-select v-model='listQuery.firstApprovalResult' size='small'>
                          <el-option
                          v-for='item in frResultArr'
                          :key='item.value'
                          :label='item.label'
                          :value='item.value'>
                          </el-option>
                      </el-select>
                  </div>
                  <!-- 初/复审完成时间 -->
                  <div class='searchItem searchItem-date'>
                      <span>{{$t('achievements.checkDate')}}</span>
                      <el-date-picker
                        size="small"
                        v-model="apprDate"
                        type="daterange"
                        :range-separator="$t('btn.go')"
                        :start-placeholder="$t('rule.beginDate')"
                        :end-placeholder="$t('rule.endDate')"
                        value-format="yyyy-MM-dd"
                        @change="dateChangeCheckTime"
                      ></el-date-picker>
                  </div>
              </div>
              <div class='menu-title-btn'>
                <button type='button' @click='search'>
                    {{$t('btn.query')}}
                </button>
                <button type='button' @click='reset'>
                    {{$t('btn.reset')}}
                </button>
            </div>
          </div>
      </el-card>
      <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
          <div class='title'>
              {{$t('approval.title2')}}
          </div>
          <div class='menu-list'>
            <div class="left">
              <!-- 审批 -->
              <button type='button' @click='approvalFn' :disabled='isDisable2'>{{$t('approval.appr')}}</button>
              <!-- 领取任务 -->
              <button type='button' @click='receive'>{{$t('approval.task')}}</button>
            </div>
              <div class="statistics">
                <!-- 被退回数量 -->
                <el-badge :value="statistics.datumBack" class="item">
                  <el-button size="small">{{$t('approval.QuantityReturned')}}</el-button>
                </el-badge>
                <!-- 我的完成数量 -->
                <el-badge :value="statistics.userCheck" class="item">
                  <el-button size="small">{{$t('approval.userCheck')}}</el-button>
                </el-badge>
                <!-- 待审核数量 -->
                <el-badge :value="statistics.notGet" class="item">
                  <el-button size="small">{{$t('approval.notGet')}}</el-button>
                </el-badge>
              </div>
          </div>
          <el-table
            v-loading="listLoading"
            ref='multipleTable'
            :data='tableData'
            tooltip-effect='dark'
            style='width: 100%'
            @selection-change='handleSelectionChange'
            border
            stripe
            size='mini'>
            <el-table-column
            header-align='center'
            align='center'
            type='selection'
            width='55'>
            </el-table-column>
            <!-- 业务ID -->
            <el-table-column
            prop='orderId' align="center" header-align="center" 
            :label="$t('approval.orderId')"
            width='140'>
            </el-table-column>
            <!-- 客户姓名 -->
            <el-table-column
            prop='realName' align="center" header-align="center" 
            :label="$t('approval.realName')"
            width='180'>
            </el-table-column>
            <!-- 手机号 -->
            <el-table-column
            prop='appUserName' align="center" header-align="center" 
            :label="$t('approval.phoneNo')"
            width="180">
            </el-table-column>
            <!-- 审批人 -->
            <el-table-column
            prop='businessUserName' align="center" header-align="center" 
            :label="$t('achievements.loanUser')"
            width='140'>
            </el-table-column>
            <!-- 产品名称 -->
            <el-table-column
            prop='proName' align="center" header-align="center" 
            :label="$t('approval.proName')"
            width='100'>
            </el-table-column>
            <!-- 金额 -->
            <el-table-column
            prop='applyAmt' align="center" header-align="center" 
            :label="$t('approval.applyAmt')"
            width='100'>
            </el-table-column>
            <!-- 申请时间 -->
            <el-table-column
            prop='crtTime' align="center" header-align="center" 
            :label="$t('approval.time')"
            width="200">
            </el-table-column>
            <!-- 初/复审完成时间 -->
            <el-table-column
            prop='checkDate' align="center" header-align="center" 
            :label="$t('achievements.checkDate')"
            width="200">
            </el-table-column>
            <!-- 初/复审批结果 -->
            <el-table-column
            prop='checkUserStatus' align="center" header-align="center" 
            :label="$t('approval.firstReviewResult')"
            width='180'>
            <template slot-scope="scope">
                <span v-if="scope.row.checkUserStatus=== 1">{{$t('approval.adopt')}}</span>
                <span v-if="scope.row.checkUserStatus=== 2">{{$t('approval.refuse')}}</span>
                <span v-if="scope.row.checkUserStatus=== 3">{{$t('approval.derate')}}</span>
            </template>
            </el-table-column>
            <!-- 业务渠道 -->
            <el-table-column
            prop='appShortName' align="center" header-align="center" 
            :label="$t('approval.channel')"
            width='130'>
            </el-table-column>
            <!-- 审批结果 -->
            <el-table-column
            prop='status' align="center" header-align="center" 
            :label="$t('approval.result')"
            width='130'>
            <template slot-scope="scope">
                <span v-if="scope.row.status=== 1">{{$t('approval.audit')}}</span>
                <span v-if="scope.row.status=== 2">{{$t('approval.credit')}}</span>
                <span v-if="scope.row.status=== 3">{{$t('approval.repayments')}}</span>
                <span v-if="scope.row.status=== 4">{{$t('approval.refused')}}</span>
                <span v-if="scope.row.status=== 5">{{$t('approval.clear')}}</span>
            </template>
            </el-table-column>
            <!-- 资料退回原因 -->
            <el-table-column
            prop='orderBackReason' align="center" header-align="center" 
            :label="$t('approval.orderBackReason')"
            width="200">
            </el-table-column>
            <!-- 审批意见 -->
            <el-table-column
            prop='checkOpinion' align="center" header-align="center" 
            :label="$t('approval.checkOpinion')"
            width='300'
            show-overflow-tooltip
            >
            </el-table-column>
          </el-table>
          <div class="pagination-container">
              <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="listQuery.page"
              :page-sizes="[5, 10, 20, 30]"
              :page-size="listQuery.limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
              </el-pagination>
          </div>
      </el-card>
      <el-dialog :title="$t('approval.task')" :visible.sync="dialogFormVisible" width="40%"
  :before-close="handleClose">
        <div style="width: 100%; display: flex; justify-content: center;">
          <!-- 任务类型 -->
          <el-form style="width: 100%;" :model="form" :rules="rules" ref="form" label-width="100px">
            <el-form-item :label="$t('approval.taskType')" prop="taskType">
              <el-radio-group v-model="form.taskType">
                <el-radio
                v-for='item in taskTypeArr'
                :key='item.pType'
                :label='item.pType'>
                {{item.pType + '(' + item.count + $t('approval.piece') + ')'}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </div>
         <div slot="footer" class="dialog-footer" style="text-align: center;">
            <el-button type="primary" @click="submitForm('form')">{{$t('btn.confirm')}}</el-button>
            <el-button @click="resetForm('form')">{{$t('btn.cancel')}}</el-button>
         </div>
      </el-dialog>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getTask, approvalData, selectPtermCount } from "api/trust/firstApproval/index";
import { page } from "api/trust/endApproval/index";
export default {
  data() {
    return {
      apprDate: [],
      applyDate: [],
      taskTypeArr: [],
      dialogFormVisible: false,
      form: {
        taskType: ''
      },
      rules: {
        taskType: [
          {
            required: true,
            message: this.$t("approval.ruleTaskType"),
            trigger: "change"
          }
        ]
      },
      statistics: {},
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 5,
        orderId: "",
        realName: "",
        phoneNo: "",
        approvalResult: 4,
        delStatus: "",
        beginDate: "",
        endDate: "",
        checkBeginDate: "",
        checkEndDate: "",
        dataReturn: "",
        firstApprovalResult: 0
      },
      total: null,
      tableData: [],
      multipleSelection: [],
      result: [
        {
          value: 0,
          label: this.$t("rule.all")
        },
        {
          value: 1,
          label: this.$t("approval.adopt")
        },
        {
          value: 2,
          label: this.$t("approval.refuse")
        },
        // {
        //   value: 3,
        //   label: this.$t('approval.derate')
        // },
        {
          value: 4,
          label: this.$t("approval.audit")
        }
      ],
      frResultArr: [
        {
          value: 0,
          label: this.$t("rule.all")
        },
        {
          value: 1,
          label: this.$t("approval.adopt")
        },
        {
          value: 2,
          label: this.$t("approval.refuse")
        },
        {
          value: 3,
          label: this.$t('approval.derate')
        }
      ],
      returnArr: [
        {
          value: 1,
          label: this.$t("approvalDetail.yes")
        },
        {
          value: 2,
          label: this.$t("approvalDetail.no")
        }
      ],
      isDisable2: true
    };
  },
  computed: {
    ...mapGetters(["elements", "endApprPage"])
  },
  created() {
    //统计
    this.countFn();
    if (this.endApprPage.has(this.$route.path)) {
      this.listQuery = this.endApprPage.get(this.$route.path);
    }
    this.getList();
  },
  methods: {
    //统计
    countFn() {
      let obj = {};
      obj.type = "3";
      approvalData(obj).then(res => {
        this.statistics = res.data;
      });
    },
    // 初审完成时间
    dateChangeCheckTime(val) {
      if (val === undefined || val === null || val === '') {
        this.listQuery.checkBeginDate = '';
        this.listQuery.checkEndDate = '';
      } else {
        this.listQuery.checkBeginDate = val[0];
        this.listQuery.checkEndDate = val[1];
      }
    },
    // 申请时间
    dateChangeApplyDate(val) {
      if (val === undefined || val === null || val === '') {
        this.listQuery.beginDate = '';
        this.listQuery.endDate = '';
      } else {
        this.listQuery.beginDate = val[0];
        this.listQuery.endDate = val[1];
      }
    },
    handleClose(done) {
      done();
      this.$refs["form"].resetFields();
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          let obj = {};
          obj.type = '3';
          // if (this.form.taskType === this.$t('rule.all')) {
          //   obj.detailType = '1';
          //   obj.pTerm = '0';
          // } else if (this.form.taskType === this.$t('approval.NoRenewal')) {
          //   obj.detailType = '2';
          //   obj.pTerm = '2';
          // } else if (this.form.taskType === this.$t('approval.renewal')) {
          //   obj.detailType = '3';
          //   obj.pTerm = '1';
          // }
          getTask(obj)
          .then(response => {
            if (response.status === 200) {
              this.$message({
                showClose: true,
                message: response.message,
                type: "success"
              });
              this.getList();
              this.countFn();
            } else {
              this.$message({
                showClose: true,
                message: response.message,
                type: "warning"
              });
            }
            // if (response.data.apply === 0) {
            //   this.$message({
            //     showClose: true,
            //     message: this.$t('approval.error'),
            //     type: 'warning'
            //   });
            //   this.getList()
            //   this.countFn();
            // } else if (response.data.apply === 1) {
            //   this.$message({
            //     showClose: true,
            //     message: this.$t('approval.success'),
            //     type: 'success'
            //   });
            //   this.getList()
            //   this.countFn();
            // } else if (response.data.apply === 2) {
            //   this.$message({
            //     showClose: true,
            //     message: this.$t('approval.most'),
            //     type: 'warning'
            //   });
            //   this.getList()
            //   this.countFn();
            // } else if (response.data === '') {
            //   this.$message({
            //     showClose: true,
            //     message: response.message,
            //     type: 'warning'
            //   });
            //   this.getList()
            //   this.countFn();
            // }
            if (this.tableData.length > 0) {
              this.isDisable2 = false;
            }
          })
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    // 点击领取任务
    receive() {
      this.form.taskType = '';
      this.taskTypeArr = [];
      let obj = {};
      obj.type = '3';
      selectPtermCount(obj)
      .then(res => {
        if (res.status === 200) {
          res.data.forEach(item => {
            if (item.pType === '0') {
              item.pType = this.$t('rule.all');
              this.taskTypeArr.push(item)
            }
          })
          this.dialogFormVisible = true;
        }
      })
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.orderId = "";
      this.listQuery.realName = "";
      this.listQuery.phoneNo = "";
      this.listQuery.approvalResult = 4;
      this.applyDate = [];
      this.apprDate = [];
      this.listQuery.checkBeginDate = '';
      this.listQuery.checkEndDate = '';
      this.listQuery.beginDate = '';
      this.listQuery.endDate = '';
      this.listQuery.dataReturn = "";
      this.listQuery.firstApprovalResult = 0;
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch("saveEndApprPage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      if (
        (this.tableData.length > 0 && this.listQuery.dataReturn === "") ||
        this.listQuery.dataReturn === 2
      ) {
        this.isDisable2 = false;
      } else {
        this.isDisable2 = true;
      }
      page(this.listQuery).then(response => {
        response.data.rows.forEach(item => {
          let re = /(?=(?!\b)(\d{3})+$)/g;
          let str = String(item.applyAmt);
          item.applyAmt = str.replace(re, ",");
        });
        if (response.status === 200) {
          this.listLoading = false;
          this.tableData = response.data.rows;
          this.total = Number(response.data.total);
        }
      });
    },
    //选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
      if (this.multipleSelection.length !== 0) {
        if (
          this.multipleSelection[0].checkStatus > 45 ||
          this.multipleSelection[0].status === 4 ||
          this.multipleSelection[0].attr8 === "1"
        ) {
          this.isDisable2 = true;
        } else {
          this.isDisable2 = false;
        }
        // if (this.multipleSelection[0].checkStatus > 45 || this.multipleSelection[0].status === 4 ) {
        //   this.isDisable2 = true;
        // } else {
        //   this.isDisable2 = false;
        // }
      }
    },
    //审批
    approvalFn() {
      if (this.multipleSelection.length == 1) {
        this.$store.dispatch("setEndApprDetails", this.multipleSelection[0]);
        this.$router.push({
          path: "/trust/endApprDetails",
          name: "终审详情 - Beta"
        });
      } else if (this.multipleSelection.length <= 0) {
        this.$message({
          showClose: true,
          message: this.$t("approval.msg1"),
          type: "warning"
        });
      } else {
        this.$message({
          showClose: true,
          message: this.$t("approval.msg2"),
          type: "warning"
        });
      }
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>
<style>
.el-dialog__body {
  padding: 20px 20px 0 20px;
}
</style>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 220px;
    }
  }
}

.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  .left {
    button {
      padding: 0 20px;
      height: 28px;
      background: #20a0ff;
      color: #fff;
      outline: none;
      border-width: 0;
      border-radius: 4px;
      font-size: 12px;
      margin-left: 10px;
      &:nth-child(1) {
        margin-left: 0;
      }
    }
    button[disabled] {
      padding: 0 20px;
      height: 28px;
      background: #c1e0ff;
      color: #fafbff;
      outline: none;
      border-width: 0;
      border-radius: 4px;
      font-size: 12px;
    }
  }
}
.menu-list {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.statistics {
  div {
    margin-right: 40px;
    font-size: 16px;
  }
}
</style>